﻿/****---------------------------------------------------------------------------------------------------->>>>
*	LoadVars2 for Flash 8 [v1.0]
*	Coded to work with ActionScript 2.0
*	--------------------------------------------------
*	Created by
*		Lance Den (vistaden@msn.com , yanjun@vika.cn)
*		SIAN Digital Technology co.,ltd. (www.vika.cn)
*
*	Based on code by
*		Luar, Yen Chin Suo(luar@luar.net)
*
*	Version History:
*		2007-04-12 - Estabished by Lance Den.
*
*	Authors notes:
*		Class Initialization:
*			- this is a Private Class
* 			- this is a core class for the project, it's very important.
*
*			Example:
*				null
*
*			Note:
*				null
*
*			Example:
*				
*
*		- Lance Den 2007-04-13
<<<<----------------------------------------------------------------------------------------------------****/

import mx.events.EventDispatcher;

dynamic class mx.utils.LoadVars2 extends LoadVars {
	public  var timeout:Number = 60000;
	private var _timeoutID:Number;
	public  var listener:Object;
	private var oldAddEventListener:Function;
	public  var addEventListener:Function;
	public  var removeEventListener:Function;
	private var dispatchEvent:Function;
	private static var _mixin1 = EventDispatcher.initialize(LoadVars2.prototype);
	
	/*------------------------------------------------------------------------>
	* Constructor
	<------------------------------------------------------------------------*/
	function LoadVars2() {
		super();
		oldAddEventListener = addEventListener;
		addEventListener = _addEventListener;
		listener = new Object();
		ASSetPropFlags(this, null, 1, false);
	}
	
	/*------------------------------------------------------------------------>
	* Public Method
	<------------------------------------------------------------------------*/
	public function load(url:String):Void
	{
		setTimeout();
		super.load(url);
	}
	
	public function sendAndLoad(url:String, targetObject:Object, method:String):Void
	{
		setTimeout();
		super.sendAndLoad(url, this, method);
	}
	
	/*------------------------------------------------------------------------>
	* Event Handler
	<------------------------------------------------------------------------*/
	private function onTimeout():Void
	{
		clearTimeout();
		dispatchEvent({target:this, type:"onDataLoaded", success:false, description:"超时"});
		removeEventListener("onDataLoaded", listener);
	}
	
	private function onLoad(success:Boolean):Void
	{
		clearTimeout();
		var str_tmp:String = (success==true) ? "成功" : "失败，网络不通或文件不存在.";
		dispatchEvent({target:this, type:"onDataLoaded", success:success, description:str_tmp});
	}
	
	// Private Method
	private function setTimeout():Void
	{
		clearTimeout();
		_timeoutID = setInterval(this, "onTimeout", timeout);
	}
	
	private function clearTimeout():Void
	{
		clearInterval(_timeoutID);
		delete _timeoutID;
	}
	
	private function _addEventListener(evtName:String, obj:Object):Void
	{
		listener = obj;
		oldAddEventListener(evtName, obj);
	}
}
